package net.tv90.bfmccore.util.area;

import net.tv90.bfmccore.util.Cooldown;
import net.tv90.bfmccore.util.vector.Vector3i;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static net.tv90.bfmccore.helper.FactionHelper.getCountry2;

public class CombatArea{
    //战斗区域所属的地图
    public String world;
    //战斗区域所属阵营,只有该阵营的玩家才能进这个区域
    public String bypassFaction;
    //玩家处在战斗区域外最多可停留的时间
    public int time;
    //战斗区域列表
    public List<Area> areaList;
    //储存倒计时
    public Map<Player, Cooldown> timeLeft;

    public CombatArea(String world, String bypassFaction, int time, List<Area> areaList) {
        this.world = world;
        this.bypassFaction = bypassFaction;
        this.time = time;
        this.areaList = areaList;
        this.timeLeft = new HashMap<>();
    }

    /**
     * 玩家是否脱离战斗区域
     * @param player 玩家
     * @param combatArea 战斗区域
     * @return 是或否
     */
    public static boolean isOutOfCombatArea(Player player, CombatArea combatArea){
        if(combatArea == null || combatArea.areaList == null) return false;
        Vector3i playerPos = new Vector3i((int) player.getLocation().getX(), (int) player.getLocation().getY(), (int) player.getLocation().getZ());
        String playerFaction = getCountry2(player);
        if(!player.getWorld().getName().equalsIgnoreCase(combatArea.world) || !playerFaction.equalsIgnoreCase(combatArea.bypassFaction)) {
            return false;
        }
        else{
            //遍历Area列表，发现玩家处在其中一个Area中就返回假
            for(Area area : combatArea.areaList){
                if(area.isInArea(playerPos)){
                    return false;
                }
            }
            return true;
        }
    }

    @Override
    public String toString() {
        return "CombatArea{" + "world='" + world + '\'' + ", bypassFaction='" + bypassFaction + '\'' + ", time=" + time + ", areaList=" + areaList + '}';
    }

    public String getWorld() {
        return world;
    }

    public void setWorld(String world) {
        this.world = world;
    }

    public String getBypassFaction() {
        return bypassFaction;
    }

    public void setBypassFaction(String bypassFaction) {
        this.bypassFaction = bypassFaction;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }

    public List<Area> getAreaList() {
        return areaList;
    }

    public void setAreaList(List<Area> areaList) {
        this.areaList = areaList;
    }
}
